package com.xn2001.order.web;

import com.xn2001.order.config.PatternProperties;
import com.xn2001.order.pojo.Order;
import com.xn2001.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @RefreshScope：nacos热更新文件修改后,无需重启服务,就能生效
 */
@RestController
@RequestMapping("/order")
@RefreshScope(proxyMode = ScopedProxyMode.DEFAULT)
public class OrderController {

   @Autowired
   private OrderService orderService;

    /**
     * 注入在nocas配置管理中,配置的
     * pattern.dateformat
     */
   @Value("${pattern.dateformat}")
   //@NacosValue(value = "${pattern.dateformat}",autoRefreshed = true)
   private String dateformat;

   @Autowired
   private PatternProperties patternProperties;

   @GetMapping("/now")
   private String now(){
       System.out.println("dateformat>>>>>>>>>>>>>>>>>>>>"+dateformat);
       return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat, Locale.CHINA));
   }

    @GetMapping("/now2")
    private String now2(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat(), Locale.CHINA));
    }

    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        // 根据id查询订单并返回
        //return orderService.queryOrderById(orderId);
        return orderService.queryOrderAndUserById(orderId);
    }
}
